<?php

class WordDao
{
    var $conn;

    function WordDao(&$conn) {
        $this->conn =& $conn;
    }

    function close_conn(){
        mysql_close($this->conn);
    }
    
    function insert(&$vo) {
        $lang = $this->get_lang($vo->value);
        $sql =    
            "INSERT INTO word_{$lang} (value, complexity, moderated)
            VALUES ('{$vo->value}', '{$vo->complexity}', '{$vo->moderated}')";
        mysql_query($sql, $this->conn) or die (mysql_error());
    }

    function exists_word($value) {
        $lang = $this->get_lang($value);
        $sql = 
            "SELECT value FROM word_{$lang} WHERE value='{$value}'";
        $result = mysql_query($sql, $this->conn) or die (mysql_error());

        return mysql_num_rows($result) > 0;
    }
    
    function select_word($complexity, $current_word, $words_in_use) {
        $new_word = preg_replace("/<br>/i","", $current_word);
        $lang = $this->get_lang($new_word);
        $sql = 
            "SELECT value FROM word_{$lang} WHERE moderated=true AND complexity='{$complexity}' ORDER BY RAND()";
        $result = mysql_query($sql, $this->conn) or die (mysql_error());
        
        while($row = mysql_fetch_array($result)){
            if(!isset($words_in_use[$row['value']])){
                $new_word = $row['value'];
                break;
            }
        }
        
        return $new_word;
    }

     function get_words($words_number, $complexity, $words_in_use,$lang){
        $sql = 
            "SELECT value FROM word_{$lang} WHERE moderated=true AND complexity='{$complexity}' ORDER BY RAND()";
        $result = mysql_query($sql, $this -> conn) or die (mysql_error());
        $words = array();
        $i = 0;
        
        while($row = mysql_fetch_array($result)){
            if(!isset($words_in_use[$row['value']])){
                $words[$row['value']] = 1;
                $i = $i+1;
                if($i == $words_number)
                    break;
            }
        }
        
        return $words;
    }
    function words_to_moderate(){
        $sql = 
            "SELECT id, value FROM word_eng WHERE moderated=FALSE ORDER BY id";
        $result = mysql_query($sql, $this -> conn) or die (mysql_error());
        $words = array();
        
        while($row = mysql_fetch_array($result)){
            $words[$row['value']] = 1;
        }
        $sql = 
            "SELECT id, value FROM word_rus WHERE moderated=FALSE ORDER BY id";
        $result = mysql_query($sql, $this -> conn) or die (mysql_error());
        
        while($row = mysql_fetch_array($result)){
            $words[$row['value']] = 1;
        }
        return $words;
    }
    function delete_word($word){
         $lang = $this->get_lang($word);
        $sql = 
            "DELETE FROM word_{$lang} WHERE value='{$word}'";
        $result = mysql_query($sql, $this -> conn) or die (mysql_error());
        $num = mysql_affected_rows();
        return $num;
    }
    
    function update_word($word,$complexity){
         $lang = $this->get_lang($word);
        $sql = 
            "UPDATE word_{$lang} SET complexity='{$complexity}', moderated=TRUE WHERE value='{$word}'";
        $result = mysql_query($sql, $this -> conn) or die (mysql_error());
        $num = mysql_affected_rows();
        return $num;
    }
    
    function get_lang($word){
        $lang = "";
        $word2 = $word . " ";
        
        $template_rus = "/^[абвгдеёжзийклмнопрстуфхцчшщыьъэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЫЪЭЮЯ-]*\s*$/m";
        $template_eng = "/^[a-zA-Z-]*\s*$/m";
        if (preg_match($template_rus, $word2)) {
            $lang = "rus";
        }
        
        if(preg_match($template_eng, $word2)) {
            $lang = "eng";
        }

        if(empty($lang)){
            $lang = "was_not_determined";
        }        
        return $lang;
    }
}

?>